package com.yscz.peexcel.util;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import com.yscz.peexcel.entity.ScaleExcel;
import com.yscz.peexcel.entity.ScoreExcel;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Slf4j
public class PoiExcelUtils {

    // 工作簿
    public static XSSFWorkbook workbook;
    // 工作表
    public static XSSFSheet sheet;
    // 行
    public static XSSFRow row;
    // 列
    public static XSSFCell cell;

    public static void main(String[] args) throws Exception {
        String fielName = "C:\\excel\\量表0927.xlsx";
        String sheetName = "量表详情";
        System.out.println(Integer.parseInt(readExcelData(fielName, sheetName, 2, 1).substring(0, 2)));

        String sunAndShadeScaleAlg = PoiExcelUtils.readExcelData("C:\\excel\\量表0927.xlsx", "量表详情", 6, 1);
        System.out.println("++++++++++++++"+sunAndShadeScaleAlg.equals(""));

        if (Boolean.parseBoolean(sunAndShadeScaleAlg)){
            System.out.println("++++++++++++++"+sunAndShadeScaleAlg.equals(""));
        }


//        ImportParams params = new ImportParams();
//        //设置表头的行数
//        params.setHeadRows(10);
//        List<ScaleExcel> list = ExcelImportUtil.importExcel(
//                new File("C:\\excel\\量表0927.xls"),
//                ScaleExcel.class, params);
//        System.out.println("解析到的数据长度是：" + list.size());
//        for (int i = 0; i < list.size(); i++) {
//
//            System.out.println(list.get(i).toString());
//        }

        String str = "1.0,2.0";
        String[] split = str.split(",");

        String[] strings = PoiExcelUtils.readExcelData("C:\\excel\\量表0927.xlsx", "量表详情", 1, 2).split(",");

        Long[] longs = Convert.toLongArray(strings);
//        long[] scaleProjectIds = Arrays.stream(longs).mapToLong(Integer::new).toArray();
        for (long s : longs) {
            System.out.println(s);
        }

//        ImportParams params3 = new ImportParams();
//        //设置表头的行数
//        params3.setHeadRows(1);
//        params3.setStartSheetIndex(2);
//        List<ScoreExcel> scoreExcels = ExcelImportUtil.importExcel(new File("C:\\excel\\量表0927.xlsx"), ScoreExcel.class, params3);
//        for (ScoreExcel scoreExcel : scoreExcels) {
//            System.out.println(scoreExcel);
//        }
    }

    public static String readExcelData(String fielName, String sheetName, int rowNum, int cellNum) throws Exception {
        InputStream in = new FileInputStream(fielName);
        workbook = new XSSFWorkbook(in);
        sheet = workbook.getSheet(sheetName);
        row = sheet.getRow(rowNum);
        cell = row.getCell(cellNum);
        XSSFRow row = cell.getRow();
        String cellValue = "";
        switch (cell.getCellType()) {
            case XSSFCell.CELL_TYPE_NUMERIC:
                log.info("第" + (rowNum + 1) + "行" + "第" + (cellNum + 1) + "列的值： " + String.valueOf(cell.getNumericCellValue()));
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            case XSSFCell.CELL_TYPE_STRING:
                log.info("第" + (rowNum + 1) + "行" + "第" + (cellNum + 1) + "列的值： " + cell.getStringCellValue());
                cellValue = cell.getStringCellValue();
                break;
            default:
                log.info("第" + (rowNum + 1) + "行" + "第" + (cellNum + 1) + "列的值： " + cell.getStringCellValue().equals(""));
                cellValue = cell.getStringCellValue();
                break;
        }
        return cellValue;
    }
}